<!--
 * @Descripttion: (领导带班数据项说明/Pro_auxiliary_leadershiponduty)
 * @version: (1.0)
 * @Author: (admin)
 * @Date: (2024-12-18)
 * @LastEditors: (admin)
 * @LastEditTime: (2024-12-18)
-->
<template>
  <div class="app-container">
    <!-- :model属性用于表单验证使用 比如下面的el-form-item 的 prop属性用于对表单值进行验证操作 -->
    <el-form :model="queryParams" size="small" label-position="right" inline ref="queryForm" :label-width="labelWidth"
      v-show="showSearch" @submit.native.prevent>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['proauxiliaryleadershiponduty:add']" plain icon="el-icon-plus"
          size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" :disabled="single" v-hasPermi="['proauxiliaryleadershiponduty:edit']" plain
          icon="el-icon-edit" size="mini" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" v-hasPermi="['proauxiliaryleadershiponduty:delete']" plain
          icon="el-icon-delete" size="mini" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 数据区域 -->
    <el-table :data="dataList" v-loading="loading" ref="table" border highlight-current-row @sort-change="sortChange"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column prop="id" label="领导带班编码" align="center" />
      <el-table-column prop="belongMine" label="煤矿编码" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="assistDate" label="领导带班日期" align="center" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
              <span>{{ parseTime(scope.row.assistDate,'{y}-{m}-{d}') }}</span>
            </template>
      </el-table-column>
      <el-table-column prop="weekday" label="星期" align="center" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <dict-tag :options="weekdayOptions" :value="scope.row.weekday" />
        </template>
      </el-table-column>
      <el-table-column prop="shift" label="班次" align="center">

        <template slot-scope="scope">
          <dict-tag :options="shiftOptions" :value="scope.row.shift" />
        </template>
      </el-table-column>
      <el-table-column prop="assistMan" label="带班人员" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="createDate" label="数据创建时间" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createDate) }}</span>
            </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="140">
        <template slot-scope="scope">
          <el-button size="mini" type="success" icon="el-icon-edit" title="编辑"
            @click="handleUpdate(scope.row)"></el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" title="删除"
            @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination class="mt10" background :total="total" :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改领导带班数据项说明对话框 -->
    <el-dialog :title="title" width="500px" :lock-scroll="false" :visible.sync="open">
      <el-form ref="form" :model="form" :rules="rules" :label-width="formLabelWidth">
          <el-row>
          <el-col :span="24">
            <el-form-item label="煤矿编码" prop="belongMine">
              <el-input v-model="form.belongMine" style="width: 221px;" disabled placeholder="请输入煤矿编码" />
            </el-form-item>
          </el-col>
        </el-row>
          <el-row>
          <el-col :span="24">
            <el-form-item label="领导带班日期" prop="assistDate">
              <el-date-picker v-model="form.assistDate" style="width: 221px;" type="date" placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
          <el-row>
          <el-col :span="24">
            <el-form-item label="星期" prop="weekday">
              <el-select v-model="form.weekday"  style="width: 221px;" placeholder="请选择">
                <el-option v-for="item in weekdayOptions"  :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
          <el-row>
          <el-col :span="24">
            <el-form-item label="班次" prop="shift">
              <el-select style="width: 221px;" v-model="form.shift" placeholder="请选择">
                <el-option v-for="item in shiftOptions" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
          <el-row>
          <el-col :span="24">
            <el-form-item label="带班人员" prop="assistManid">
              <el-select style="width: 221px;" filterable multiple v-model="form.assistManid" placeholder="请选择带班人员" >
                <el-option v-for="item in assistManOptions" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import {
  listduty,
  addduty,
  delduty,
  updateduty,
  getduty,
  getMan
} from '@/api/project/proLeadershiponduty.js';

export default {
  name: "proauxiliaryleadershiponduty",
  data() {
    return {

      labelWidth: "100px",
      formLabelWidth: "120px",
      // 选中uuid数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sort: undefined,
        sortType: undefined,
      },
      // 弹出层标题
      title: "",
      // 操作类型 1、add 2、edit
      opertype: 0,
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},

      // 0正常;1删除选项列表 格式 eg:
      weekdayOptions: [
        { dictLabel: '星期一', dictValue: '1' },
        { dictLabel: '星期二', dictValue: '2' },
        { dictLabel: '星期三', dictValue: '3' },
        { dictLabel: '星期四', dictValue: '4' },
        { dictLabel: '星期五', dictValue: '5' },
        { dictLabel: '星期六', dictValue: '6' },
        { dictLabel: '星期日', dictValue: '7' }],
      // 数据操作标识字典值选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
      shiftOptions: [],
      assistManOptions: [],
      // 数据列表
      dataList: [],
      // 总记录数
      total: 0,
      // 提交按钮是否显示
      btnSubmitVisible: true,
      // 表单校验
      rules: {
        belongMine: [
          { required: true, message: "煤矿编号不能为空", trigger: "blur" }
        ],
        assistDate: [
          { required: true, message: "领导带班日期不能为空", trigger: "blur" }
        ],
        weekday: [
          { required: true, message: "星期不能为空", trigger: "blur" }
        ],
        shift: [
          { required: true, message: "班次不能为空", trigger: "blur" }
        ],
        assistManid: [
          { required: true, message: "带班人员不能为空", trigger: "blur" }
        ]
      },
    };
  },
  created() {
    // 列表数据查询
    this.getList();
    this.getDicts('sys_bctype').then((response) => {
      this.shiftOptions = response.data
    })
    this.getMan();
  },
  methods: {
  //   handleAssistManChange(values) {
  //     debugger;
  //    // 遍历选中的值，找到对应的选项并添加文本值
  //    this.form.assistMan=[];
  //    values.forEach(value => {
  //     const selectedOption = this.assistManOptions.find(option => option.dictValue === value);
  //     if (selectedOption) {
  //       this.form.assistMan.push(selectedOption.dictLabel);
  //     }
  //   });
  // },
    getMan(){
      getMan().then((response) => {
      this.assistManOptions = response.data
    })
    },
    // 查询数据
    getList() {
      this.loading = true;
      listduty(this.queryParams).then(res => {
        if (res.code == 200) {
          this.dataList = res.data.result;
          this.total = res.data.totalNum;
          this.loading = false;
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 重置数据表单
    reset() {
      this.form = {
        assistManid:[],
        belongMine: undefined,
        assistDate: undefined,
        weekday: undefined,
        shift: undefined,
        assistMan: [],
        createDate: undefined,
        updateDate: undefined,
        isDelete: undefined,
        stateFlag: undefined,
      };
      this.resetForm("form");
    },
    // 重置查询操作
    resetQuery() {
      this.timeRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.uuid);
      this.single = selection.length != 1
      this.multiple = !selection.length;
    },
    // 自定义排序
    sortChange(column) {
      if (column.prop == null || column.order == null) {
        this.queryParams.sort = undefined;
        this.queryParams.sortType = undefined;
      } else {
        this.queryParams.sort = column.prop;
        this.queryParams.sortType = column.order;
      }

      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
      this.opertype = 1;
      this.getbelongMine().then((response) => {
      this.form.belongMine = response.data.sysCode
    })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.uuid || this.ids;

      this.$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？')
        .then(function () {
          return delduty(Ids);
        })
        .then(() => {
          this.handleQuery();
          this.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.uuid || this.ids;
      getduty(id).then((res) => {
        const { code, data } = res;
        if (code == 200) {
          this.open = true;
          this.title = "修改数据";
          this.opertype = 2;

          this.form = {
            ...data,
          };
          this.form.assistManid = data.assistMan.split(',');
         // this.form.assistManid = data.assistMan.split(',');
        }
      });
    },
    // 0正常;1删除字典翻译
    isDeleteFormat(row, column) {
      return this.selectDictLabel(this.isDeleteOptions, row.isDelete);
    },
    // 数据操作标识字典值字典翻译
    stateFlagFormat(row, column) {
      return this.selectDictLabel(this.stateFlagOptions, row.stateFlag);
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.assistMan=[];
          console.log(JSON.stringify(this.form));
          this.form.assistManid.forEach(value => {
      const selectedOption = this.assistManOptions.find(option => option.dictValue === value);
      if (selectedOption) {
        this.form.assistMan.push(selectedOption.dictLabel);
      }
    });
//           this.form.assistManid = this.form.assistManid1.join(',');
           this.form.assistMan=  Array.from(new Set(this.form.assistMan));
          if (this.form.uuid != undefined && this.opertype === 2) {
            updateduty(this.form)
              .then((res) => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .catch((err) => {
                //TODO 错误逻辑
              });
          } else {
            addduty(this.form)
              .then((res) => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .catch((err) => {
                //TODO 错误逻辑
              });
          }
        }
      });
    },
  },
};
</script>